/**
 * Copyright (c) 2016-2021 by the respective copyright holders.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
package com.zsmartsystems.zigbee.dongle.ember.ezsp.structure;

import java.util.HashMap;
import java.util.Map;

/**
 * Class to implement the Ember Enumeration <b>EmberJoinMethod</b>.
 * <p>
 * Note that this code is autogenerated. Manual changes may be overwritten.
 *
 * @author Chris Jackson - Initial contribution of Java code generator
 */
public enum EmberJoinMethod {
    /**
     * Default unknown value
     */
    UNKNOWN(-1),

    /**
     * Normally devices use MAC Association to join a network, which respects the "permit joining"
     * flag in the MAC Beacon. For mobile nodes this value causes the device to use an Ember Mobile
     * Node Join, which is functionally equivalent to a MAC association. This value should be used
     * by default.
     */
    EMBER_USE_MAC_ASSOCIATION(0x0000),

    /**
     * For those networks where the "permit joining" flag is never turned on, they will need to use a
     * ZigBee NWK Rejoin. This value causes the rejoin to be sent without NWK security and the Trust
     * Center will be asked to send the NWK key to the device. The NWK key sent to the device can be
     * encrypted with the device's corresponding Trust Center link key. That is determined by the
     * ::EmberJoinDecision on the Trust Center returned by the
     * ::emberTrustCenterJoinHandler(). For a mobile node this value will cause it to use an Ember
     * Mobile node rejoin, which is functionally equivalent.
     */
    EMBER_USE_NWK_REJOIN(0x0001),

    /**
     * For those networks where the "permit joining" flag is never turned on, they will need to use a
     * NWK Rejoin. If those devices have been preconfigured with the NWK key (including sequence
     * number) they can use a secured rejoin. This is only necessary for end devices since they need a
     * parent. Routers can simply use the ::EMBER_USE_NWK_COMMISSIONING join method below.
     */
    EMBER_USE_NWK_REJOIN_HAVE_NWK_KEY(0x0002),

    /**
     * For those networks where all network and security information is known ahead of time, a
     * router device may be commissioned such that it does not need to send any messages to begin
     * communicating on the network.
     */
    EMBER_USE_NWK_COMMISSIONING(0x0003);

    /**
     * A mapping between the integer code and its corresponding type to
     * facilitate lookup by code.
     */
    private static Map<Integer, EmberJoinMethod> codeMapping;

    private int key;

    static {
        codeMapping = new HashMap<Integer, EmberJoinMethod>();
        for (EmberJoinMethod s : values()) {
            codeMapping.put(s.key, s);
        }
    }

    private EmberJoinMethod(int key) {
        this.key = key;
    }

    /**
     * Lookup function based on the EmberStatus type code. Returns null if the
     * code does not exist.
     *
     * @param code the code to lookup
     * @return enumeration value of the alarm type.
     */
    public static EmberJoinMethod getEmberJoinMethod(int code) {
        if (codeMapping.get(code) == null) {
            return UNKNOWN;
        }

        return codeMapping.get(code);
    }

    /**
     * Returns the EZSP protocol defined value for this enumeration.
     *
     * @return the EZSP protocol key
     */
    public int getKey() {
        return key;
    }
}
